package com.moyuwanjia.Demo;

import com.moyuwanjia.Dao.IUserDao;
import com.moyuwanjia.Dao.UserDaoImpl;
import com.moyuwanjia.Vo.QueryVo;
import com.moyuwanjia.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author 摸鱼丸加~
 * @create 2022-03-30 21:55
 */
public class test {
    private SqlSession session;
    private InputStream inputStream;

    @Before
    public void init() throws IOException {
        //1.读取配置文件
        inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        //2.创建SqlSessionFactory工厂
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory build = builder.build(inputStream);
        //3.使用工厂生产SqlSession对象
//        session = build.openSession();
        // 默认自动提交事务
        session = build.openSession(true);
    }
    @After
    public void distory() throws IOException {
        //6.释放资源
        session.close();
        inputStream.close();
    }
    /**
     * 测试使用xml配置和注解配置的方式
     * @throws Exception
     */
    @Test
    public void test01()throws Exception{
        //1.读取配置文件
//        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        //2.创建SqlSessionFactory工厂
//        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
//        SqlSessionFactory build = builder.build(inputStream);
        //3.使用工厂生产SqlSession对象
//        SqlSession session = build.openSession();
        //4.使用SqlSession创建Dao接口的代理对象
        IUserDao mapper = session.getMapper(IUserDao.class);
        //5.使用代理对象执行方法
        List<User> all = mapper.findAll02();
//        List<User> all = mapper.findAll();
        all.forEach(System.out::println);
        //6.释放资源
//        session.close();
//        inputStream.close();

    }

    @Test
    public void test02()throws Exception{
        //1.读取配置文件
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        //2.创建SqlSessionFactory工厂
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = builder.build(inputStream);
        //3.使用工厂生产Dao对象
        IUserDao userDao = new UserDaoImpl(factory);
        //5.使用代理对象执行方法
        List<User> all = userDao.findAll();
        all.forEach(System.out::println);
        inputStream.close();
    }

    @Test
    public void test03(){
        IUserDao mapper = session.getMapper(IUserDao.class);
        int i = mapper.FindTotal();
        System.out.println(i);
    }

    /**
     * 使用Vo作为查询条件（OGNL表达式）
     * ObjectGraphicNavigationLanguage对象图导航语言
     * Mybatis使用ognl表达式解析对象字段的值，#{}或者${}括号中的值为pojo属性名
     */
    @Test
    public void test04(){
        QueryVo queryVo  = new QueryVo();
        User user = new User();
        user.setUsername("王五");

        queryVo.setUser(user);
        IUserDao mapper = session.getMapper(IUserDao.class);
        List<User> users = mapper.FindTotal1(queryVo);
        users.forEach(System.out::println);
    }
}
